package com.example.backstage.controller;


import com.example.backstage.service.ICategoryService;
import com.example.backstage.service.UserService;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@ResponseBody
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private ICategoryService iCategoryService;

    //管理员添加品类接口
    @PostMapping("/add_category.do")
    public ResultData addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        //校验是否是管理员
        if (userService.checkAdminRole(user) != null) {
            //是管理员，增加我们处理分类的逻辑
            return iCategoryService.addCategory(categoryName, parentId);
        } else {
            return ResultData.fail("无权限操作，需要管理员权限");
        }
    }

    //管理员更新品类
    @PostMapping("/set_category_name.do")
    public ResultData setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        //校验是否是管理员
        if (userService.checkAdminRole(user) != null) {
            //是管理员，修改我们处理分类的逻辑
            return iCategoryService.updateCategoryName(categoryId,categoryName);
        }else {
            return ResultData.fail("无权限操作，需要管理员权限");
        }
    }

    //获取品类子节点(平级)
    @GetMapping("/get_category.do")
    public ResultData getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue ="0" )Integer categoryId){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        //校验是否是管理员
        if (userService.checkAdminRole(user) != null) {
            //查询子节点的category信息，并且不递归，保持平级
            return  iCategoryService.getChildrenParallelCategory(categoryId);
        }else {
            return ResultData.fail("无权限操作，需要管理员权限");
        }
    }

    //查询当前节点和子节点
    @GetMapping("/get_children_category.do")
    public ResultData getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue ="0" )Integer categoryId){
        //验证用户是否登录
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("请登录管理员账户，登录后再试");
        }
        //校验是否是管理员
        if (userService.checkAdminRole(user) != null) {
            //查询当前节点的Id和递归子节点的Id
            return  iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else {
            return ResultData.fail("无权限操作，需要管理员权限");
        }
    }
}

